<html lang="en">
 <head>
  <meta charset="UTF-8" />
  <title>test</title>
  <script language="javascript" src="http://www.12301.cc/js/jquery-1.7.2.min.js"></script>
  <script type="text/javascript">   
  $(function () {  
		var MsgBox = { MsgObj: $('#msg'),showMsg: function (msg) {MsgBox.MsgObj.html(msg); }} //轮询       
		var Comet = { 
			Init: function () { 
				$('#btn').unbind();
				$('#btn').bind('click', Comet.Poll);//轮询
				},            
			Poll: function () { 
				var id = parseInt(Math.random() * 1000);//为了打日志写的东西,随机生成的
				MsgBox.showMsg(''); 
					$.ajax({ 
						url: './test.php',			//访问地址
						type: 'get',				//get方式提交
						data: {id: id},				//传入ajax数据
						dataType: 'json',			//返回格式 
						timeout: 10000,				//超时设置十秒
						success: Comet.Success, 	//成功时的返回 
						error: Comet.Failed        	//失败的时候
					});
			},
			Success: function (data) {//成功获取数据时 
				if (data.status == 1) {
					MsgBox.showMsg(data.info);
				} 
			},
			//没有获取数据 
			Failed: function (XMLHttpRequest, textStatus, errorThrown) { 
				if (textStatus == 'timeout') {//超时重新连接
					Comet.Poll(); 
				}
				else {
					Comet.Poll();
				} 
			} 
		};
		Comet.Init();
	});
	</script>
 </head>
 <body> 
  <div id="msg"></div> 
  <input type="button" value="开始" id="btn" />
 </body>
</html>